package com.diy.springframework.step;

import com.diy.springframework.step.context.ApplicationContext;
import com.diy.springframework.step.processor.BeanPostProcessor;

/**
 * @program: diy_spring
 * @description:
 * @author: CaoYong
 * @create: 2021-07-24 13:35
 **/
public class ApplicationContextAwareProcessor implements BeanPostProcessor {

    private final ApplicationContext applicationContext;

    public ApplicationContextAwareProcessor(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws Exception {
        if (bean instanceof ApplicationContextAware) {
            ((ApplicationContextAware) bean).setApplicationContext(applicationContext);
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws Exception {
        return bean;
    }
}
